package com.hurteng.stormplane;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.text.InputFilter;
import android.text.InputType;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.hurteng.stormplane.myplane.R;
import com.hurteng.stormplane.verifcode.CreatCode;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class FgpwdActivity extends Activity {

    private ImageView iv_code;

    private Button bt_submit;
    private EditText et_code;
    private EditText et_cfm;
    private EditText et_nickName;
    private EditText et_email;
    private EditText et_pwd;
    private EditText et_pwdCfm;
    private TextView tv_again;
    private TextView tv_login;


    private String realCode;
    private AlertDialog.Builder builder;
    private AlertDialog alertDialog;
    private int cfmErrCnt = 0;
    private int codeErrCnt = 0;
    private String serverCode="";
    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_fgpwd);
        iv_code = findViewById(R.id.iv_fgcode);
        bt_submit = findViewById(R.id.bt_fgsubmit);
        et_email = findViewById(R.id.et_fgemail);
        et_pwd = findViewById(R.id.et_fgpwd);
        et_pwdCfm = findViewById(R.id.et_fgpwdCfm);
        et_code = findViewById(R.id.et_fgcode);
        tv_again = findViewById(R.id.tv_fgagain);
        tv_login = findViewById(R.id.tv_fglogin);
        //获取验证码图片
        refreshCfm();
        Log.d("code", realCode);
        //刷新验证码点击事件
        tv_again.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                refreshCfm();
                Log.d("code", realCode);
            }
        });
        //重新登录点击事件
        tv_login.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent=new Intent(FgpwdActivity.this,LoginActivity.class);
                intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
                startActivity(intent);

            }
        });
        //提交键点击事件
        bt_submit.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                int info = processInfo();
                if (info != -1) {
                    switch (info) {
                        case 100:
                            Toast.makeText(FgpwdActivity.this, "昵称不能为空", Toast.LENGTH_SHORT).show();
                            break;
                        case 110:
                            Toast.makeText(FgpwdActivity.this, "邮箱地址不能为空", Toast.LENGTH_SHORT).show();
                            break;
                        case 120:
                            Toast.makeText(FgpwdActivity.this, "邮箱格式不正确", Toast.LENGTH_SHORT).show();
                            break;
                        case 130:
                            Toast.makeText(FgpwdActivity.this, "密码不能为空", Toast.LENGTH_SHORT).show();
                            break;
                        case 140:
                            Toast.makeText(FgpwdActivity.this, "验证码不能为空", Toast.LENGTH_SHORT).show();
                            break;
                        case 150:
                            Toast.makeText(FgpwdActivity.this, "密码过于简单", Toast.LENGTH_SHORT).show();
                            break;
                        case 200:
                            Toast.makeText(FgpwdActivity.this, "两次输入密码不相同", Toast.LENGTH_SHORT).show();
                            break;
                        case 210:
                            Toast.makeText(FgpwdActivity.this, "验证码错误", Toast.LENGTH_SHORT).show();
                            cfmErrCnt++;
                            if (cfmErrCnt >= 3) {
                                cfmErrCnt = 0;
                                refreshCfm();
                            }
                            break;
                    }
                    return;
                }
                // TODO: 2020/3/18 这里需要添加发送消息给服务器，同时接收服务器传回来的验证码，服务器同时给邮箱发送验证码

                builder = new AlertDialog.Builder(FgpwdActivity.this);
                et_cfm = new EditText(FgpwdActivity.this);
                et_cfm.setInputType(InputType.TYPE_CLASS_NUMBER);
                et_cfm.setFilters(new InputFilter[]{new InputFilter.LengthFilter(6)});
                builder.setCancelable(false);
                builder.setTitle("请输入收到的验证码");
                builder.setView(et_cfm);
                builder.setPositiveButton("提交", null);
                alertDialog = builder.create();
                alertDialog.show();
                alertDialog.getButton(AlertDialog.BUTTON_POSITIVE).setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View view) {
                        if (!et_cfm.getText().toString().equals(serverCode)) {
                            codeErrCnt++;
                            Toast.makeText(FgpwdActivity.this, "输入错误" + codeErrCnt + "次/共3次机会", Toast.LENGTH_SHORT).show();
                            if (codeErrCnt >= 3) {
                                codeErrCnt = 0;
                            } else {
                                return;
                            }

                        } else {
                            Toast.makeText(FgpwdActivity.this, "注册成功！", Toast.LENGTH_SHORT).show();
                        }
                        refreshCfm();
                        alertDialog.dismiss();
                    }
                });

            }
        });
    }

    public static boolean isEmail(String string) {
        if (string == null)
            return false;
        String regEx1 = "^([a-z0-9A-Z]+[-|\\.]?)+[a-z0-9A-Z]@([a-z0-9A-Z]+(-[a-z0-9A-Z]+)?\\.)+[a-zA-Z]{2,}$";
        Pattern p;
        Matcher m;
        p = Pattern.compile(regEx1);
        m = p.matcher(string);
        if (m.matches())
            return true;
        else
            return false;
    }

    private void refreshCfm() {
        iv_code.setImageBitmap(CreatCode.getInstance().createBitmap());
        realCode = CreatCode.getInstance().getCode().toLowerCase();
    }

    private int processInfo() {
        String email = et_email.getText().toString().trim();
        String pwd = et_pwd.getText().toString().trim();
        String pwdCfm = et_pwdCfm.getText().toString().trim();
        String code = et_code.getText().toString().trim().toLowerCase();
        if (email.equals("")) {
            return 110;
        }
        if (!FgpwdActivity.isEmail(email)) {
            return 120;
        }
        if (pwd.equals("")) {
            return 130;
        }
        if (pwd.length() < 6) {
            return 150;
        }
        if (!pwd.equals(pwdCfm)) {
            return 200;
        }
        if (code.equals("")) {
            return 140;
        }
        if (!code.equals(realCode)) {
            return 210;
        }

        return -1;
    }
}
